home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 235 / Issue 235 - September 2007 - DPCS0907DVD.ISO / Extras / NetObjects Fusion / NOF10.exe / data1.cab / FSI / lib / nof / text / DelimitedTextTable.js < prev    next >
Encoding:
Text File  |  2007-04-11  |  3.5 KB  |  118 lines

  1.     /****i* SOURCE_FILE/INFO
  2.         *
  3.         * NAME
  4.         *  DelimitedTextTable.js
  5.         *
  6.         * USAGE
  7.         *  Part of Netobjects JavaScript Library.
  8.         *
  9.         * COPYRIGHT
  10.         *  Copyright ⌐ 2000-2005 Website Pros, Inc.
  11.         *  All Rights Reserved.
  12.         *
  13.         *  This is an unpublished work protected by Website Pros, Inc.
  14.         *  as a trade secret, and is not to be used or disclosed except as
  15.         *  expressly provided in a written license agreement executed by
  16.         *  you and Website Pros, Inc.
  17.         *
  18.         *      <copyright@websitepros.com>
  19.         *
  20.         * NOTES
  21.         *  JavaScript code.
  22.         *
  23.         *****/
  24.     if (!IS.isModuleInitialized("IS.NOF.TEXT.DelimitedTextTable"))
  25.     { 
  26.         
  27.         /****h* NOF_JavaScript_Library/NOF.TEXT.DelimitedTextTable
  28.         *
  29.         * NAME
  30.         *  NOF.TEXT.DelimitedTextTable
  31.         *
  32.         * DESCRIPTION
  33.         *
  34.         * <code>DelimitedTextTable</code> is a class which 
  35.         * Usage sample:
  36.         * External dependencies: none.
  37.         ****/
  38.         
  39.         /**
  40.         * Constructor
  41.         **/
  42.         function TEXT_DelimitedTextTable(/*String*/ delimiter, /*String*/ lineSeparator) {
  43.             this.__proto__ = TEXT_DelimitedTextTable.prototype;
  44.             
  45.             this.delimiter        = delimiter;
  46.             this.lineSeparator    = (lineSeparator) ? lineSeparator : "\n";
  47.         }
  48.         {
  49.             var method = TEXT_DelimitedTextTable.prototype;
  50.             
  51.             /**
  52.             * parse a delimited file content and get the table as list of lines
  53.             * containing the formated values
  54.             * @param fileContent
  55.             **/
  56.             method.parse = function (/*string*/ fileContent) {
  57.                 if (fileContent == null || fileContent.length == 0) return null;
  58.                 //alert(fileContent);
  59.                 
  60.                 var lines = fileContent.split(this.lineSeparator);
  61.                 var l = lines.length;
  62.                 if (l == 0)
  63.                     return null;
  64.                 
  65.                 var tbl = new Array();
  66.                 
  67.                 var i = -1;
  68.                 var line, tmpStr;
  69.                 var candidateColValues;
  70.                 var iOf, j;
  71.                 
  72.                 while (++i < l) {
  73.                     line = lines[i];
  74.                     if (line.trim().length == 0) continue;
  75.                     //log.info("line " + i + " = " + line, "DelimitedTextTable", "parse");
  76.                     var colValues = new Array();    //line.split("\t");
  77.                     
  78.                     //parseLine()
  79.                     if (line.indexOf('""') > -1) {
  80.                         line = line.replaceSubstr('""', "__DQUOTE__");
  81.                     }
  82.                     candidateColValues = line.split(this.delimiter);
  83.                     //log.info("candidate columns " + candidateColValues.length, "DelimitedTextTable", "parse");
  84.                     for (j=0; j < candidateColValues.length; j++) {
  85.                         tmpStr = candidateColValue = candidateColValues[j];
  86.                         iOf = candidateColValue.indexOf('"');
  87.                         //log.info(candidateColValue, "DelimitedTextTable", "parse");
  88.                         //log.info("indexOf = " + iOf, "DelimitedTextTable", "parse");
  89.                         if ((iOf > -1) && (candidateColValue.indexOf('"', iOf + 1) < 0)) {
  90.                             while ((candidateColValues[++j].indexOf('"') < 0) && (j < candidateColValues.length)) {
  91.                                 tmpStr += this.delimiter + candidateColValues[j];
  92.                             }
  93.                             tmpStr += this.delimiter + candidateColValues[j];                            
  94.                         }
  95.                         if (tmpStr.indexOf('"') == 0) {                            
  96.                             tmpStr = tmpStr.substr(1, tmpStr.length - 2);
  97.                             //tmpStr = tmpStr.substring(1, tmpStr.length - 1);
  98.                         }
  99.                         if (tmpStr.indexOf("__DQUOTE__") > -1) {
  100.                             tmpStr = tmpStr.replaceSubstr("__DQUOTE__", '\"');
  101.                         }
  102.                         //log.info("added = " + tmpStr, "DelimitedTextTable", "parse");
  103.                         colValues[colValues.length] = tmpStr;
  104.                     }                    
  105.                     //end parseLine    
  106.                     
  107.                     tbl[tbl.length] = colValues;
  108.                     
  109.                 }
  110.                 
  111.                 return tbl;
  112.             }
  113.             
  114.         }
  115.         
  116.         // add it to NOF.TEXT namespace
  117.         NOF.TEXT.__proto__.DelimitedTextTable = TEXT_DelimitedTextTable;
  118.     }